package com.example.materialtest

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.core.view.GravityCompat
import androidx.recyclerview.widget.GridLayoutManager
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.activity_main.*
import kotlin.concurrent.thread

class MainActivity : AppCompatActivity() {
    private val fruits = mutableListOf(
        Fruit("fruit1", R.mipmap.fruit1),
        Fruit("fruit2", R.mipmap.fruit2),
        Fruit("fruit3", R.mipmap.fruit3),
        Fruit("fruit4", R.mipmap.fruit4),
        Fruit("fruit5", R.mipmap.fruit5),
        Fruit("fruit6", R.mipmap.fruit6),
        Fruit("fruit7", R.mipmap.fruit7),
        Fruit("fruit8", R.mipmap.fruit8),
        Fruit("fruit9", R.mipmap.fruit9),
        Fruit("fruit10", R.mipmap.fruit10),
        Fruit("fruit11", R.mipmap.fruit11),
        Fruit("fruit12", R.mipmap.fruit12),
        Fruit("fruit13", R.mipmap.fruit13),
        Fruit("fruit14", R.mipmap.fruit14),
        Fruit("fruit15", R.mipmap.fruit15),
        Fruit("fruit16", R.mipmap.fruit16),
        Fruit("fruit17", R.mipmap.fruit17),
        Fruit("fruit18", R.mipmap.fruit18),
        Fruit("fruit19", R.mipmap.fruit19),
        Fruit("fruit20", R.mipmap.fruit20),
        Fruit("fruit21", R.mipmap.fruit21),
        Fruit("fruit22", R.mipmap.fruit22)
    )

    private val fruitList = arrayListOf<Fruit>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)
        supportActionBar?.let {
            it.setDisplayHomeAsUpEnabled(true)
            it.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp)
        }

        navView.setCheckedItem(R.id.navCall)
        navView.setNavigationItemSelectedListener {
            drawerLayout.closeDrawers()
            true
        }

        fab.setOnClickListener {
            //Toast.makeText(this, "扫地机佛i阿胶", Toast.LENGTH_SHORT).show()
            Snackbar.make(it, "Data Deleted", Snackbar.LENGTH_SHORT)
                .setAction("Undo") {
                    Toast.makeText(this, "Data Restored", Toast.LENGTH_SHORT).show()
                }
                .show()
        }

        initFruits()
        val layoutManager = GridLayoutManager(this, 2)
        recyclerView.layoutManager = layoutManager
        val adapter = FruitAdapter(this, fruitList)
        recyclerView.adapter = adapter

        swipeRefresh.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent)
        swipeRefresh.setOnRefreshListener {
            refreshFruits(adapter)
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.toolbar, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.backup -> Toast.makeText(this, "You clicked Backup", Toast.LENGTH_SHORT).show()
            R.id.delete -> Toast.makeText(this, "You clicked Delete", Toast.LENGTH_SHORT).show()
            R.id.settings -> Toast.makeText(this, "You clicked Settings", Toast.LENGTH_SHORT).show()
            android.R.id.home -> drawerLayout.openDrawer(GravityCompat.START)
        }
        return true
    }

    private fun initFruits() {
        fruitList.clear()
        repeat(50) {
            val index = (0 until fruits.size).random()
            fruitList.add(fruits[index])
        }
    }

    private fun refreshFruits(adapter: FruitAdapter) {
        thread {
            Thread.sleep(2000)
            runOnUiThread {
                initFruits()
                adapter.notifyDataSetChanged()
                swipeRefresh.isRefreshing = false
            }
        }
    }
}
